package com.css.gdzj.gdzjbxgd.common.util.export;

import freemarker.template.Template;

import java.io.*;
import java.nio.file.Files;
import java.util.Map;

public class ExportWordUtils {
    public static File createDoc(Map<?,?>  dataMap, Template template){
        //File f=new File("/data/word/userInfo.doc");
        File f=new File("D:/userInfo.doc");
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            template.process(dataMap,writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return  f;
    }

    private static void copyFileUsingJava7Files(File source, File dest)
            throws IOException {
        Files.copy(source.toPath(), dest.toPath());
    }

    public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) { //文件存在时
                InputStream inStream = new FileInputStream(oldPath); //读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ( (byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; //字节数 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        }
        catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
        }
    }

    private static void copyFileUsingFileStreams(File source, File dest)
            throws IOException {
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }
        } finally {
            input.close();
            output.close();
        }
    }

    public static void methon5(String fileName, String copyFileName)
            throws IOException {
        // 创建输入缓冲流对象
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        // 创建输出缓冲流对象
        BufferedWriter writer = new BufferedWriter(new FileWriter(copyFileName));
        // 一次写一个字符
        // char[] chs=new char[1024];
        String line;
        while ((line = reader.readLine()) != null) {
            writer.write(line);
            writer.newLine();// 换行
            writer.flush();// 清理缓冲区域
        }
        // 释放资源
        reader.close();
        writer.close();
        System.out.println("第5种方式：利用缓冲流的特有方法一次读写一个字符串贝文件完成！");
    }



    public static void main(String[] args) throws IOException {
/*        File oldPath=new File("D:/userInfo2.doc");
        File newPath=new File("D:/newUserInfo.docx");
        try {
            copyFileUsingJava7Files(oldPath,newPath);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

 //       copyFile("D:/userInfo2.doc","D:/newUserInfo2.docx");

/*        File oldPath=new File("D:/userInfo2.doc");
        File newPath=new File("D:/newUserInfo3.docx");
        try {
            copyFileUsingFileStreams(oldPath,newPath);
        } catch (IOException e) {
            e.printStackTrace();
        }*/

/*        FileReader fr = new FileReader("D:\\userInfo2.doc");
        FileWriter fw = new FileWriter("D:\\newUserInfo4.docx");
        char[] c = new char[1024];
        int len = 0;
        while ((len = fr.read(c)) != -1) {
            fw.write(c, 0, len);
            fw.flush();
        }
        fw.close();
        fr.close();
        System.out.println("复制完成");*/

        methon5("D:/userInfo2.doc","D:/newUserInfo5.docx");
    }

}
